package com.legendshop.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

//@Controller
@ControllerAdvice
public class ErrorController1 {

    /**
     * The log.
     */
    private final static Logger log = LoggerFactory.getLogger(ErrorController1.class);

    /**
     * 统一异常处理
     *
     * @param exception exception
     * @return
     */
    @ExceptionHandler({RuntimeException.class})
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(RuntimeException exception) {
        log.info("自定义异常处理-RuntimeException");
        ModelAndView m = new ModelAndView();
        m.addObject("errorMessage", exception.getMessage());
        m.addObject("stackTrace", exception.getStackTrace());
        m.setViewName("error/error1");
        return m;
    }

    /**
     * 统一异常处理
     *
     * @param exception exception
     * @return
     */
    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(Exception exception) {
        log.info("自定义异常处理-Exception");
        ModelAndView m = new ModelAndView();
        m.addObject("errorMessage", exception.getMessage());
        m.addObject("stackTrace", exception.getStackTrace());
        m.setViewName("error/error2");
        return m;
    }
}
